//@author A0096950U
#include "Logger.h"

const string Logger::logFilename = "PowerScheduleLog.txt";
const LogLevel Logger::defaultLogTreshold = LOG_INFO;

bool Logger::instanceFlag = false;
Logger* Logger::instance = NULL;

Logger& Logger::getInstance() {
	if(!instanceFlag)
	{
		instance = new Logger(); 
		instanceFlag = true;
	}

	return *instance;
}

Logger::Logger(void) {
	logFile.open(logFilename);
	setLogTreshold(defaultLogTreshold);
}


Logger::~Logger(void) {
	logFile.close();
}

void Logger::log(LogLevel logLevel, string message) {
	if(logLevel <= logTreshold)
	{
		logFile << logLevelToString(logLevel) << ": ";
		logFile << message << endl;
		logFile.flush();
	}
}

void Logger::setLogTreshold(LogLevel logLevel) {
	logTreshold = logLevel;
}

string Logger::logLevelToString(LogLevel logLevel) {
	string toReturn;

	switch(logLevel)
	{
	case LOG_ERROR:
		toReturn = "LOG_ERROR";
		break;
	case LOG_WARNING:
		toReturn = "LOG_WARNING";
		break;
	case LOG_INFO:
		toReturn = "LOG_INFO";
		break;
	case LOG_DEBUGGING:
		toReturn = "LOG_DEBUGGING";
		break;
	default:
		toReturn = "LOG";
		break;
	}

	return toReturn;
}